﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CooStationsToolsNew
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static GridSplitter leftspl = null;
        public static GridSplitter rightspl = null;
        public static WorkMainAreaUC workmainuc = null;
        public static WorkLeftAreaUC workleftuc = null;
        public static WorkRightAreaUC workrightuc = null;
        public static WorkMenuAreaUC workmenuuc = null;
        public static Point leftpt = new Point();
        public static Point rightpt = new Point();
        ProjectTools pt = new ProjectTools();
        Release release = new Release();
        public MainWindow()
        {
            InitializeComponent();
            if (CheckWindowsVersion() == "Windows7")
            {
                this.MaxHeight = SystemParameters.WorkArea.Height + 6;
            }
            else
            { 
                this.MaxHeight = SystemParameters.WorkArea.Height;
            }
            //清楚Temp目录数据
            pt.ClearTemp();
        }

        private void window_Loaded(object sender, RoutedEventArgs e)
        {
            
            InitWorkArea();
            release.GetList();
            App.pageName = "cfy";
            InitWindow inw = new InitWindow();
            inw.Owner = this;
            if (inw.ShowDialog() == true)
            {
                leftspl = leftgridSplitter;
                rightspl = rightgridSplitter;
                workmainuc.InitWorkMainArea();
                workrightuc.InitActionCombox();
                workleftuc.InitPageList();
                workleftuc.BindingTreeView(WorkMainAreaUC.workcanvas);
            }
           // GetSplitterPoint();
        }

        public void InitNewPage(string pageName)
        {
            App.pageName = pageName;
            InitWorkArea();
            PageTools paget = new PageTools();
            paget.OpenPage();
           
                leftspl = leftgridSplitter;
                rightspl = rightgridSplitter;
                workmainuc.InitWorkMainArea();
                workrightuc.InitActionCombox();
                workleftuc.InitPageList();
                workleftuc.BindingTreeView(WorkMainAreaUC.workcanvas);
           
        }

        public static void GetSplitterPoint()
        { 
            Point p = new Point(0, 0);
            leftpt = leftspl.PointToScreen(p);
            rightpt = rightspl.PointToScreen(p);
        }

        /// <summary>
        /// 初始化界面
        /// </summary>
        public void InitWorkArea()
        {
            try
            {
                if (workmainuc != null)
                {
                    grid1.Children.Remove(workmainuc);
                    workmainuc = null;
                }
                if (workleftuc != null)
                {
                    grid1.Children.Remove(workleftuc);
                    workleftuc = null;
                }
                if (workrightuc != null)
                {
                    grid1.Children.Remove(workrightuc);
                    workrightuc = null;
                }
                if (workmenuuc != null)
                {
                    grid1.Children.Remove(workmenuuc);
                    workmenuuc = null;
                }
                //初始化工作区
                WorkMainAreaUC wmain = new WorkMainAreaUC(this);
                grid1.Children.Add(wmain);
                wmain.SetValue(Grid.RowProperty, 2);
                wmain.SetValue(Grid.ColumnProperty, 2);
                workmainuc = wmain;
                //初始化对象栏
                WorkLeftAreaUC wleft = new WorkLeftAreaUC(this);
                grid1.Children.Add(wleft);
                wleft.SetValue(Grid.RowProperty, 2);
                wleft.SetValue(Grid.ColumnProperty, 0);
                workleftuc = wleft;
                //初始化属性栏
                WorkRightAreaUC wright = new WorkRightAreaUC(this);
                grid1.Children.Add(wright);
                wright.SetValue(Grid.RowProperty, 2);
                wright.SetValue(Grid.ColumnProperty, 4);
                workrightuc = wright;
                //初始化菜单栏
                WorkMenuAreaUC wmenu = new WorkMenuAreaUC(this);
                grid1.Children.Add(wmenu);
                wmenu.SetValue(Grid.RowProperty, 1);
                wmenu.SetValue(Grid.ColumnSpanProperty, 5);
                workmenuuc = wmenu;
            }
            catch {
                MessageBox.Show("初始化失败");
            }
        }


        /// <summary>
        /// 获得windows版本
        /// </summary>
        /// <returns></returns>
        public static string CheckWindowsVersion()
        {
            string str = null;
            OperatingSystem os = Environment.OSVersion;
            switch (os.Platform)
            {
                case PlatformID.Win32Windows:
                    switch (os.Version.Minor)
                    {
                        case 0:
                            str = "Windows   95 ";
                            break;
                        case 10:
                            if (os.Version.Revision.ToString() == "2222A ")
                                str = "Windows   98   第二版 ";
                            else
                                str = "Windows   98 ";
                            break;
                        case 90:
                            str = "Windows   Me ";
                            break;
                    }
                    break;
                case PlatformID.Win32NT:
                    switch (os.Version.Major)
                    {
                        case 3:
                            str = "Windows   NT   3.51 ";
                            break;
                        case 4:
                            str = "Windows   NT   4.0 ";
                            break;
                        case 5:
                            switch (os.Version.Minor)
                            {
                                case 0:
                                    str = "Windows   200 ";
                                    break;
                                case 1:
                                    str = "WindowsXP ";
                                    break;
                                case 2:
                                    str = "Windows   2003 ";
                                    break;
                            }
                            break;
                        case 6:
                            switch (os.Version.Minor)
                            {
                                case 0:
                                    str = "Windows  Vista ";
                                    break;
                                case 1:
                                    str = "Windows7";
                                    break;
                            }
                            break;
                    }
                    break;
            }
            return str;
        }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                this.WindowState = (this.WindowState == System.Windows.WindowState.Normal ? System.Windows.WindowState.Maximized : System.Windows.WindowState.Normal);
            }
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void lbtn_back_Click(object sender, RoutedEventArgs e)
        {

        }
        //关闭工程
        private void lbtn_close_Click(object sender, RoutedEventArgs e)
        {
            
            MessageForm2 mf = new MessageForm2("是否保存后关闭程序！");
            mf.Owner = this;

            if (mf.ShowDialog() == true)
            {
                ProgressWin pw = new ProgressWin();
                pw.Owner = this;
                pw.ShowDialog();
                App.Current.Shutdown();
            }
            else
            {
                if (mf.flag == false)
                {
                }
                else
                {
                    App.Current.Shutdown();
                }
            }
        }

        private void lbtn_max_Click(object sender, RoutedEventArgs e)
        {
            if (CheckWindowsVersion() == "Windows7")
            {
                this.MaxHeight = SystemParameters.WorkArea.Height + 6;
            }
            else
            {
                this.MaxHeight = SystemParameters.WorkArea.Height;
            }
            Style style1 = (Style)this.FindResource("StyleBtn5");
            Style style2 = (Style)this.FindResource("StyleBtn5_other");
            this.WindowState = (this.WindowState == System.Windows.WindowState.Normal ? System.Windows.WindowState.Maximized : System.Windows.WindowState.Normal);
            lbtn_max.Style = (lbtn_max.Style == style1 ? style2 : style1);
        }

        private void lbtn_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        private void window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void window_PreviewKeyUp(object sender, KeyEventArgs e)
        {

        }

        private void window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            //判断text控件的 输入焦点
            if (ModelTools.isleave == 2)
            {
                Keyboard.ClearFocus();
                ModelTools.isleave = 0;
            }
        }
    }
}
